//
//  GKZYHomeInfoAreaView.m
//  GKZY_CLASS
//
//  Created by KwokChun Cheung on 15/11/27.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "GKZYHomeInfoAreaView.h"
#import "xxptmobile.h"

#pragma mark - OOHomeInfoModel
@implementation GKZYHomeInfoModel

@end

#pragma mark - OOHomeInfoAreaView

@interface GKZYHomeInfoAreaView()

@end

@implementation GKZYHomeInfoAreaView{
    UIColor *colorForNumber;
    CGFloat sizeForNumber;
    
    UIColor *colorForWord;
    CGFloat sizeForWord;
    
    CGFloat widthForArea;
    CGFloat heightForArea;
}

-(instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        
        colorForNumber = [UIColor colorWithRed:79/255.0 green:79/255.0 blue:79/255.0 alpha:1];
        colorForWord = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        
        sizeForNumber = 16;
        sizeForWord = 11;
        
        widthForArea = 0.25;
        heightForArea = 15;
        
//        [self addSubview:self.countCourseArea];
//        [self addGapLineWith:self.countCourseArea on:LinePositionRight];
//        [self.countCourseArea mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self);
//            make.left.equalTo(self);
//            make.width.equalTo(self).multipliedBy(widthForArea);
//            make.height.equalTo(@(heightForArea));
//        }];
//        
//        [self addSubview:self.countCompleteArea];
//        [self addGapLineWith:self.countCompleteArea on:LinePositionRight];
//        [self.countCompleteArea mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self);
//            make.left.equalTo(self.countCourseArea.mas_right);
//            make.width.equalTo(self).multipliedBy(widthForArea);
//            make.height.equalTo(@(heightForArea));
//        }];
//        
//        [self addSubview:self.totolScoreArea];
//        [self addGapLineWith:self.totolScoreArea on:LinePositionRight];
//        [self.totolScoreArea mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self);
//            make.left.equalTo(self.countCompleteArea.mas_right);
//            make.width.equalTo(self).multipliedBy(widthForArea);
//            make.height.equalTo(@(heightForArea));
//        }];
//        
//        [self addSubview:self.getScoreArea];
//        [self.getScoreArea mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self);
//            make.left.equalTo(self.totolScoreArea.mas_right);
//            make.right.equalTo(self);
//            make.width.equalTo(self).multipliedBy(widthForArea);
//            make.height.equalTo(@(heightForArea));
//        }];
        
    }
    return self;
}

#pragma mark - Setter For Property

-(void)setInfoArray:(NSMutableArray<GKZYHomeInfoModel *> *)infoArray{
    _infoArray = infoArray;
    if (infoArray && infoArray.count >= 1) {
        NSArray *subViews = [self subviews];
        for (UIView *view in subViews) {
            [view removeFromSuperview];
        }
        
        NSInteger modelCount = infoArray.count;
        CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width/modelCount;
        for (int i = 0; i < modelCount; i++) {
            GKZYHomeInfoModel *infoModel = infoArray[i];
            UIView *infoView = [UIView new];
            infoView.layer.masksToBounds = YES;
            UILabel *detailLabel = [UILabel new];
            detailLabel.textAlignment = NSTextAlignmentCenter;
            detailLabel.font = [UIFont systemFontOfSize:sizeForWord];
            detailLabel.textColor = colorForWord;
            detailLabel.text = infoModel.detail;
            [detailLabel sizeToFit];
            [infoView addSubview:detailLabel];
            
            UILabel *infoLabel = [UILabel new];
            infoLabel.textAlignment = NSTextAlignmentCenter;
            infoLabel.font = [UIFont systemFontOfSize:sizeForNumber];
            infoLabel.textColor = colorForNumber;
            infoLabel.text = infoModel.info;
            [infoLabel sizeToFit];
            [infoView addSubview:infoLabel];
            [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(infoView.mas_centerY).offset(-heightForArea/2);
                make.centerX.equalTo(infoView.mas_centerX);
                make.height.mas_equalTo(@(heightForArea));
                make.width.lessThanOrEqualTo(@(10)).priorityMedium();
            }];
            [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(infoLabel.mas_bottom).offset(5);
                make.centerX.equalTo(infoView.mas_centerX);
                make.height.mas_equalTo(@(heightForArea));
                make.width.lessThanOrEqualTo(@(10)).priorityMedium();
            }];
            
            [self addSubview:infoView];
            [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(15);
                make.bottom.equalTo(self.mas_bottom).offset(-15);
                make.left.equalTo(self.mas_left).offset(viewWidth*i);
                make.width.mas_equalTo(@(viewWidth));
            }];
            
            if (i != modelCount - 1) {
                [self addGapLineWith:infoView on:LinePositionRight];
            }
        }
    }
}

#pragma mark - Tools Methods

typedef NS_ENUM(NSUInteger, LinePosition) {
    LinePositionTop,
    LinePositionLeft,
    LinePositionRight,
    LinePositionBottom,
};
-(void)addGapLineWith:(UIView *)view on:(LinePosition)posit{
    //用于防止重复插入
    NSInteger tagNumber = 960;
    switch (posit) {
        case LinePositionTop:
            tagNumber = 961;
            break;
        case LinePositionLeft:
            tagNumber = 962;
            break;
        case LinePositionRight:
            tagNumber = 963;
            break;
        case LinePositionBottom:
            tagNumber = 964;
            break;
        default:
            break;
    }
    UIView *exist = [view viewWithTag:tagNumber];
    if (exist) { return; }
    
    CGFloat lineSizeUnit = 0.5;
    UILabel *line = [UILabel new];
    line.backgroundColor = [UIColor lightGrayColor];
    line.tag = tagNumber;
    
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        if (posit==LinePositionRight || posit==LinePositionLeft) {
            if (posit==LinePositionRight) {
                make.right.equalTo(view);
            }else if (posit==LinePositionLeft){
                make.left.equalTo(view);
            }
            make.top.equalTo(view);
            make.height.equalTo(view);
            make.width.equalTo(@(lineSizeUnit));
        }else if (posit==LinePositionTop || posit==LinePositionBottom){
            if (posit==LinePositionTop) {
                make.top.equalTo(view);
            }else if (posit==LinePositionBottom){
                make.bottom.equalTo(view);
            }
            make.height.equalTo(@(lineSizeUnit));
            make.width.equalTo(view);
        }
    }];
}
@end
